package com.liangge.essaysite.task;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.TaskExecutor;

import com.liangge.essaysite.service.ArticleService;
import com.liangge.essaysite.spider.GoodmoodSpider;
import com.liangge.essaysite.util.AgentKeys;
import com.liangge.essaysite.util.CfgUtils;
import com.liangge.essaysite.util.HttpClientUtils;

/**
 * 爬虫Task
 * @author liangge
 */
public class SpiderTask {
	private static Logger logger = LoggerFactory.getLogger(SpiderTask.class);
	private TaskExecutor taskExecutor;
	private ArticleService articleService;
	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}
	public void setTaskExecutor(TaskExecutor taskExecutor) {
		this.taskExecutor = taskExecutor;
	}

	/**
	 * 好心情散文 prose.goodmood.cn
	 * @author liangge
	 */
	public void doGoodmoodSpider(){
		logger.info("goodmood spider is running!");
		//爬虫开关
		boolean spiderSwitch = CfgUtils.getCommonCfg().getBoolean(AgentKeys.P_GOODMOOD_CN_SPIDER_SWITCH);
		if(spiderSwitch == false){
			logger.info("goodmood spider switch is tern off!");
			return;
		}
		String goodmoodUrl = CfgUtils.getCommonCfg().getString(AgentKeys.P_GOODMOOD_CN_URL);
		//抓取HTML代码
		String goodmoodHtml = HttpClientUtils.httpGet(goodmoodUrl);
		//解析HTML代码
		Document document = Jsoup.parse(goodmoodHtml);
		Elements elements = document.select("div.a_n_d td");
		for(Iterator<Element> it = elements.iterator();it.hasNext();){
			Element element = it.next().select("a").get(1);
			String href = element.attr("href");
			int articleId = Integer.parseInt(StringUtils.substringBetween(href,"_", "."));
			//查询该文章是否存在
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("articleId", articleId);
			params.put("articleSource", AgentKeys.ARTICLE_SOURCE_GOODMOOD);
			int exists = articleService.selectArticleCountByKeys(params);
			if(exists == 0){
				String articleUrl = goodmoodUrl + href;
				taskExecutor.execute(new GoodmoodSpider(articleService,articleUrl));
			}
		}
	}
}
